package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.PayConstants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.mapper.CourseOrderMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "OrderPayConsumer",
        topic = PayConstants.TOPIC_PAY_ORDER_DELAY_MESSAGE,
        selectorExpression = PayConstants.TAGS_PAY_ORDER_DELAY_MESSAGE
)
public class OrderPayConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private CourseOrderMapper courseOrderMapper;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String orderNo = new String(body, Constants.UTF8);
            CourseOrder courseOrder = courseOrderMapper.selectOne(new LambdaQueryWrapper<CourseOrder>()
                    .eq(CourseOrder::getOrderNo, orderNo).eq(CourseOrder::getStatusOrder, RConst.Num.BASE_LONG_0));
            if(courseOrder != null){
                courseOrder.setStatusOrder(RConst.Num.INT_2);
                courseOrderMapper.updateById(courseOrder);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
